<template>
  <el-dialog :visible.sync="visible" v-dialogDrag :title="title" :close-on-click-modal="false" class="tl-dialog" width="500px" @open="open" @close="close" :append-to-body="true" :modal-append-to-body="true">
    <el-radio-group v-model="backNodeId" class="customize-back-list">
      <el-radio :label="item.id" v-for="item in datas" :key="item.id">{{ item.name }}</el-radio>
    </el-radio-group>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="doBack" :loading="saveLoading">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
/** mixins */
import { mixin_base } from '@tapui/mixins/mixin_base'
/** 子组件 */
/** 工具类 */
import { putAction, getAction, postAction, deleteAction } from '@tapui/utils/http'
/** VUE配置 */
export default {
  components: {},
  mixins: [mixin_base],
  data() {
    return {
      title: '选择要退回的节点',
      datas: [],
      visible: false,
      backNodeId: 0,
      saveLoading: false,
      url: {
        // 额外的url
        list: '/base/flow/queryCustomizeBackNode',
        back: '/base/flow/customizeBack'
      }
    }
  },
  props: {
    // 关联流程
    sourceType: {
      default: '',
      type: String
    },
    flowInsId: {
      default: 0,
      type: [String, Number]
    }
  },
  methods: {
    open() {
      this.queryData()
    },
    queryData() {
      getAction(this.url.list, { flowinsid: this.flowInsId }).then((res) => {
        if (res.success) {
          this.datas = res.result
        } else {
          this.$tap.alert(res.message)
        }
      })
    },
    doBack() {
      if (!this.backNodeId) {
        this.$tap.alert('请选择退回的节点')
        return
      }
      let postData = {
        flowinsid: this.flowInsId,
        sourcetype: this.sourceType,
        backnodeid: this.backNodeId,
        userid: this.user.id
      }
      postAction(this.url.back, postData).then((res) => {
        if (res.success) {
          this.$tap.message('退回成功')
          this.$emit('refreshApplyEdit')
          this.visible = false
        } else {
          this.$tap.alert(res.message)
        }
      })
    },
    close() {
      this.datas = []
      this.backNodeId = 0
      this.saveLoading = false
    }
  }
}
</script>
<style lang="scss">
.customize-back-list {
  margin-top: 10px;
  .el-radio {
    display: block;
    margin-bottom: 10px;
  }
}
</style>
